{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第二章习题\n",
    "\n",
    "### 一、填空题\n",
    "\n",
    "1.列表的`sort()`方法没有返回值,或者说返回值为___None_____."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2.已知列表 `data=[1,2,30 4]`，那么`data[2:100]`的值为__[30, 4]______."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.己知`x=3`和`y=5`,那么执行语句`x,y=y,x`之后，`y`的值为____3____."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4．已知字典 `data= {'a;97, 'A' :65}`,那么`data.get('a', None)`的值为____97____."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 二、判断题\n",
    "\n",
    "1.生成器表达式的计算结果是一个元组。`(    )`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "False"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2．包含列表的元组可以作为字典的“键”。`(    )`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "False"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3．包含列表的元组不可以作为集合的元素。`(    )`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "True"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4．列表的`rindex()`方法返回指定元素在列表中最后一次出现的位置。`(    )`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "False"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 三、编程题\n",
    "\n",
    "1.输入一个字符串，输出其中每个字符的出现次数。要求使用标准库`collotections`中的`Counter`类,请自行查阅相关用法。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "输入一个字符串'goodbye world'\n"
     ]
    }
   ],
   "source": [
    "s=eval(input('输入一个字符串'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.count('g')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.count('o')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.count('d')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.count('b')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.count('y')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.count('e')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.count('w')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.count('r')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.count('l')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2．输入一个字符串,输出其中只出现了一次的字符及其下标。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "输入一个字符串'1,2,6,5,6,5,96,5,6,7'\n"
     ]
    }
   ],
   "source": [
    "s=eval(input('输入一个字符串'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "y = [(x, index) for index, x in enumerate(s) if s.index(x)==s.rindex(x)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[('1', 0), ('2', 2), ('9', 12), ('7', 19)]\n"
     ]
    }
   ],
   "source": [
    "print(y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3．输入一个字符串,输出其中每个唯一字符最后一次出现的下标。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一个字符串'1,6,8,9,9,8,9,9'\n"
     ]
    }
   ],
   "source": [
    "s=eval(input('请输入一个字符串'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "y =[(x,index) for index,x in enumerate(s)\n",
    "if index == s.rindex(x)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[('1', 0), ('6', 2), ('8', 10), (',', 13), ('9', 14)]\n"
     ]
    }
   ],
   "source": [
    "print(y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4．输入包含若干集合的列表，输出这些集合的并集。提示:使用`reduce()`函数和`operator`模块中的运算实现多个集合的并集。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "from functools import reduce\n",
    "from operator import __or__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入包含若干集合的列表[{12,55},{45,69}]\n"
     ]
    }
   ],
   "source": [
    "s=eval(input('请输入包含若干集合的列表'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{45, 12, 69, 55}\n"
     ]
    }
   ],
   "source": [
    "print(reduce(__or__,s))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "5．输入一个字符串,检查该字符串是否为回文（正着读和反看读都一样的字符串)，如果是就输出Yes，否则输出No。要求使用切片实现"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一个字符串'145665'\n"
     ]
    }
   ],
   "source": [
    "s=eval(input('请输入一个字符串'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "No\n"
     ]
    }
   ],
   "source": [
    "x=s[::-1]\n",
    "if s==x:\n",
    "    print('Yes')\n",
    "else:\n",
    "    print('No')\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 课本上65页课后习题"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1.Python 3.x的range()函数返回一个__range对象_________."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2.表达式“[3] in [1,2,3,4]”的值为___False________."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.列表对象的sort()方法用来对列表元素进行原地排序,该函数返回值为_____None____."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4．列表对象的_______remove_______方法删除首次出现的指定元素，如果列表中不存在要删除的元素，则抛出异常。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "5.假设列表对象aList的值为[3,4,5,6,7,9,11,13,15,17]，_那么切片aList[3:7]得到的值是_____[6,7,9,11]______."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "6.在Python中,字典和集合都是用一对__大括号__作为界定符,字典的每个元素由两部分组成,即__键___和___值___,其中___键__不允许重复。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "7.使用字典对象的___item()___方法可以返回字典的“键:值”对，使用字典对象的__keys()___方法可以返回字典的“键”,使用字典对象的__values()___方法可以返回字典的‘值’"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "8.假设有列表a=['name', 'age', 'sex ]和b=[ 'Dong' ,38, 'Male'],使用一条语句将这两个列表的内容转换为字典,并且以列表a中的元素为“键”,以列表b中的元素为“值”，这个语句可以写为______c=dict(zip(a,b))_______."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "9.假设有一个列表a,现要求从列表a中每3个元素取1个，并且将取到的元素组成新的列表b,可以使用语句________b=a[::3]_________________."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "10.使用列表推导式生成包含10个数字5的列表,语句可以写为______[5 for i in range (10)]__________。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "11._____不可以______(可以、不可以)使用del命令来删除元组中的部分元素"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "12.已知列表data中有若干字符串,要求编写程序,对data中的字符串进行过滤，只输出重复字符不超过一半的字符串。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['bye', 'good', 'world']\n"
     ]
    }
   ],
   "source": [
    "from collections import Counter     \n",
    "data = ['bye','good','world']\n",
    "for d in data:\n",
    "    res=Counter(d)\n",
    "    max_len=0\n",
    "    for key in res:\n",
    "        if max_len<res[key]:\n",
    "            max_len=res[key]\n",
    "    n=len(d)\n",
    "    if max_len>n/2:\n",
    "        data.remove(d)\n",
    "print(data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "13.编写程序,使用筛选法查找并输出小于1000的所有素数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def prime(x):                       \n",
    "    for i in range(2,x):\n",
    "        if x%i==0:\n",
    "            return False\n",
    "        if i==x-1:\n",
    "            return True\n",
    "output=filter(prime,range(2,1001))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997]\n"
     ]
    }
   ],
   "source": [
    "print(list(output))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "14.为什么应尽量从列表的尾部进行元素的增加与删除操作?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "列表的insert（）方法可以在列表的任意位置插入元素，但由于列表的自动内存管理功能，insert（）方法会涉及插入位置之后所有元素的移动，会影响处理速度"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "15.编写程序,生成包含1000个0~100的随机整数,并统计每个元素的出现次数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "74 : 15\n",
      "51 : 9\n",
      "52 : 10\n",
      "68 : 7\n",
      "73 : 13\n",
      "12 : 12\n",
      "15 : 13\n",
      "34 : 11\n",
      "50 : 11\n",
      "26 : 9\n",
      "88 : 8\n",
      "68 : 7\n",
      "25 : 10\n",
      "73 : 13\n",
      "36 : 15\n",
      "7 : 9\n",
      "61 : 13\n",
      "83 : 8\n",
      "1 : 7\n",
      "35 : 11\n",
      "45 : 13\n",
      "84 : 10\n",
      "2 : 12\n",
      "96 : 13\n",
      "37 : 11\n",
      "59 : 8\n",
      "11 : 11\n",
      "64 : 20\n",
      "64 : 20\n",
      "75 : 7\n",
      "52 : 10\n",
      "37 : 11\n",
      "6 : 13\n",
      "71 : 10\n",
      "91 : 7\n",
      "60 : 14\n",
      "74 : 15\n",
      "98 : 14\n",
      "82 : 9\n",
      "17 : 9\n",
      "8 : 14\n",
      "9 : 7\n",
      "75 : 7\n",
      "49 : 11\n",
      "53 : 9\n",
      "66 : 13\n",
      "14 : 9\n",
      "5 : 8\n",
      "91 : 7\n",
      "98 : 14\n",
      "89 : 16\n",
      "81 : 8\n",
      "30 : 14\n",
      "69 : 10\n",
      "60 : 14\n",
      "14 : 9\n",
      "96 : 13\n",
      "12 : 12\n",
      "64 : 20\n",
      "26 : 9\n",
      "17 : 9\n",
      "87 : 4\n",
      "40 : 9\n",
      "90 : 7\n",
      "41 : 12\n",
      "39 : 11\n",
      "18 : 8\n",
      "73 : 13\n",
      "87 : 4\n",
      "39 : 11\n",
      "67 : 9\n",
      "72 : 15\n",
      "40 : 9\n",
      "98 : 14\n",
      "13 : 9\n",
      "63 : 13\n",
      "21 : 9\n",
      "85 : 11\n",
      "46 : 11\n",
      "10 : 10\n",
      "65 : 4\n",
      "96 : 13\n",
      "20 : 8\n",
      "73 : 13\n",
      "64 : 20\n",
      "55 : 18\n",
      "96 : 13\n",
      "9 : 7\n",
      "74 : 15\n",
      "99 : 20\n",
      "13 : 9\n",
      "45 : 13\n",
      "1 : 7\n",
      "100 : 7\n",
      "47 : 4\n",
      "84 : 10\n",
      "72 : 15\n",
      "61 : 13\n",
      "6 : 13\n",
      "64 : 20\n",
      "55 : 18\n",
      "91 : 7\n",
      "94 : 10\n",
      "60 : 14\n",
      "81 : 8\n",
      "50 : 11\n",
      "99 : 20\n",
      "76 : 5\n",
      "100 : 7\n",
      "31 : 10\n",
      "42 : 11\n",
      "39 : 11\n",
      "3 : 9\n",
      "86 : 11\n",
      "54 : 13\n",
      "55 : 18\n",
      "97 : 8\n",
      "45 : 13\n",
      "99 : 20\n",
      "61 : 13\n",
      "93 : 5\n",
      "69 : 10\n",
      "42 : 11\n",
      "85 : 11\n",
      "61 : 13\n",
      "94 : 10\n",
      "60 : 14\n",
      "96 : 13\n",
      "45 : 13\n",
      "53 : 9\n",
      "10 : 10\n",
      "85 : 11\n",
      "89 : 16\n",
      "39 : 11\n",
      "62 : 8\n",
      "46 : 11\n",
      "6 : 13\n",
      "39 : 11\n",
      "33 : 10\n",
      "57 : 5\n",
      "68 : 7\n",
      "50 : 11\n",
      "63 : 13\n",
      "89 : 16\n",
      "71 : 10\n",
      "72 : 15\n",
      "15 : 13\n",
      "35 : 11\n",
      "32 : 11\n",
      "50 : 11\n",
      "8 : 14\n",
      "40 : 9\n",
      "36 : 15\n",
      "15 : 13\n",
      "15 : 13\n",
      "10 : 10\n",
      "0 : 8\n",
      "72 : 15\n",
      "23 : 13\n",
      "52 : 10\n",
      "48 : 6\n",
      "3 : 9\n",
      "6 : 13\n",
      "74 : 15\n",
      "15 : 13\n",
      "97 : 8\n",
      "35 : 11\n",
      "15 : 13\n",
      "63 : 13\n",
      "21 : 9\n",
      "45 : 13\n",
      "71 : 10\n",
      "40 : 9\n",
      "90 : 7\n",
      "10 : 10\n",
      "100 : 7\n",
      "0 : 8\n",
      "7 : 9\n",
      "27 : 12\n",
      "95 : 12\n",
      "36 : 15\n",
      "29 : 9\n",
      "14 : 9\n",
      "20 : 8\n",
      "42 : 11\n",
      "55 : 18\n",
      "55 : 18\n",
      "13 : 9\n",
      "55 : 18\n",
      "89 : 16\n",
      "88 : 8\n",
      "53 : 9\n",
      "91 : 7\n",
      "54 : 13\n",
      "85 : 11\n",
      "46 : 11\n",
      "86 : 11\n",
      "0 : 8\n",
      "51 : 9\n",
      "64 : 20\n",
      "45 : 13\n",
      "16 : 10\n",
      "65 : 4\n",
      "95 : 12\n",
      "82 : 9\n",
      "37 : 11\n",
      "93 : 5\n",
      "23 : 13\n",
      "78 : 11\n",
      "12 : 12\n",
      "16 : 10\n",
      "10 : 10\n",
      "79 : 10\n",
      "59 : 8\n",
      "79 : 10\n",
      "3 : 9\n",
      "41 : 12\n",
      "36 : 15\n",
      "31 : 10\n",
      "70 : 9\n",
      "94 : 10\n",
      "98 : 14\n",
      "99 : 20\n",
      "28 : 5\n",
      "54 : 13\n",
      "76 : 5\n",
      "15 : 13\n",
      "18 : 8\n",
      "29 : 9\n",
      "64 : 20\n",
      "99 : 20\n",
      "82 : 9\n",
      "72 : 15\n",
      "49 : 11\n",
      "37 : 11\n",
      "75 : 7\n",
      "64 : 20\n",
      "53 : 9\n",
      "36 : 15\n",
      "54 : 13\n",
      "51 : 9\n",
      "94 : 10\n",
      "46 : 11\n",
      "25 : 10\n",
      "90 : 7\n",
      "35 : 11\n",
      "84 : 10\n",
      "14 : 9\n",
      "35 : 11\n",
      "42 : 11\n",
      "96 : 13\n",
      "15 : 13\n",
      "40 : 9\n",
      "38 : 6\n",
      "64 : 20\n",
      "34 : 11\n",
      "95 : 12\n",
      "52 : 10\n",
      "53 : 9\n",
      "12 : 12\n",
      "34 : 11\n",
      "98 : 14\n",
      "81 : 8\n",
      "2 : 12\n",
      "44 : 13\n",
      "96 : 13\n",
      "14 : 9\n",
      "65 : 4\n",
      "38 : 6\n",
      "60 : 14\n",
      "68 : 7\n",
      "7 : 9\n",
      "44 : 13\n",
      "51 : 9\n",
      "56 : 6\n",
      "96 : 13\n",
      "60 : 14\n",
      "34 : 11\n",
      "42 : 11\n",
      "42 : 11\n",
      "21 : 9\n",
      "4 : 7\n",
      "37 : 11\n",
      "55 : 18\n",
      "86 : 11\n",
      "89 : 16\n",
      "64 : 20\n",
      "49 : 11\n",
      "78 : 11\n",
      "27 : 12\n",
      "20 : 8\n",
      "19 : 6\n",
      "93 : 5\n",
      "96 : 13\n",
      "72 : 15\n",
      "66 : 13\n",
      "63 : 13\n",
      "98 : 14\n",
      "55 : 18\n",
      "44 : 13\n",
      "46 : 11\n",
      "97 : 8\n",
      "66 : 13\n",
      "77 : 6\n",
      "86 : 11\n",
      "58 : 7\n",
      "72 : 15\n",
      "33 : 10\n",
      "11 : 11\n",
      "8 : 14\n",
      "83 : 8\n",
      "11 : 11\n",
      "69 : 10\n",
      "25 : 10\n",
      "52 : 10\n",
      "30 : 14\n",
      "86 : 11\n",
      "21 : 9\n",
      "45 : 13\n",
      "99 : 20\n",
      "98 : 14\n",
      "98 : 14\n",
      "39 : 11\n",
      "63 : 13\n",
      "14 : 9\n",
      "44 : 13\n",
      "44 : 13\n",
      "88 : 8\n",
      "12 : 12\n",
      "45 : 13\n",
      "27 : 12\n",
      "8 : 14\n",
      "98 : 14\n",
      "1 : 7\n",
      "13 : 9\n",
      "41 : 12\n",
      "55 : 18\n",
      "41 : 12\n",
      "81 : 8\n",
      "67 : 9\n",
      "44 : 13\n",
      "89 : 16\n",
      "42 : 11\n",
      "25 : 10\n",
      "78 : 11\n",
      "69 : 10\n",
      "99 : 20\n",
      "3 : 9\n",
      "99 : 20\n",
      "21 : 9\n",
      "44 : 13\n",
      "13 : 9\n",
      "16 : 10\n",
      "12 : 12\n",
      "58 : 7\n",
      "70 : 9\n",
      "78 : 11\n",
      "42 : 11\n",
      "70 : 9\n",
      "28 : 5\n",
      "60 : 14\n",
      "10 : 10\n",
      "79 : 10\n",
      "96 : 13\n",
      "43 : 8\n",
      "38 : 6\n",
      "61 : 13\n",
      "42 : 11\n",
      "75 : 7\n",
      "84 : 10\n",
      "45 : 13\n",
      "26 : 9\n",
      "30 : 14\n",
      "61 : 13\n",
      "24 : 3\n",
      "26 : 9\n",
      "2 : 12\n",
      "99 : 20\n",
      "63 : 13\n",
      "31 : 10\n",
      "33 : 10\n",
      "33 : 10\n",
      "1 : 7\n",
      "98 : 14\n",
      "67 : 9\n",
      "23 : 13\n",
      "85 : 11\n",
      "59 : 8\n",
      "99 : 20\n",
      "25 : 10\n",
      "73 : 13\n",
      "76 : 5\n",
      "92 : 6\n",
      "92 : 6\n",
      "48 : 6\n",
      "46 : 11\n",
      "92 : 6\n",
      "19 : 6\n",
      "42 : 11\n",
      "95 : 12\n",
      "32 : 11\n",
      "35 : 11\n",
      "45 : 13\n",
      "69 : 10\n",
      "18 : 8\n",
      "74 : 15\n",
      "23 : 13\n",
      "22 : 13\n",
      "72 : 15\n",
      "86 : 11\n",
      "60 : 14\n",
      "78 : 11\n",
      "97 : 8\n",
      "48 : 6\n",
      "23 : 13\n",
      "84 : 10\n",
      "29 : 9\n",
      "59 : 8\n",
      "16 : 10\n",
      "11 : 11\n",
      "22 : 13\n",
      "11 : 11\n",
      "73 : 13\n",
      "22 : 13\n",
      "55 : 18\n",
      "55 : 18\n",
      "68 : 7\n",
      "67 : 9\n",
      "27 : 12\n",
      "88 : 8\n",
      "60 : 14\n",
      "56 : 6\n",
      "8 : 14\n",
      "34 : 11\n",
      "89 : 16\n",
      "3 : 9\n",
      "60 : 14\n",
      "22 : 13\n",
      "41 : 12\n",
      "67 : 9\n",
      "45 : 13\n",
      "78 : 11\n",
      "46 : 11\n",
      "5 : 8\n",
      "87 : 4\n",
      "34 : 11\n",
      "9 : 7\n",
      "17 : 9\n",
      "30 : 14\n",
      "10 : 10\n",
      "9 : 7\n",
      "81 : 8\n",
      "56 : 6\n",
      "34 : 11\n",
      "0 : 8\n",
      "17 : 9\n",
      "70 : 9\n",
      "64 : 20\n",
      "33 : 10\n",
      "6 : 13\n",
      "8 : 14\n",
      "57 : 5\n",
      "35 : 11\n",
      "81 : 8\n",
      "66 : 13\n",
      "82 : 9\n",
      "78 : 11\n",
      "72 : 15\n",
      "74 : 15\n",
      "63 : 13\n",
      "23 : 13\n",
      "90 : 7\n",
      "97 : 8\n",
      "19 : 6\n",
      "66 : 13\n",
      "67 : 9\n",
      "49 : 11\n",
      "85 : 11\n",
      "41 : 12\n",
      "62 : 8\n",
      "13 : 9\n",
      "67 : 9\n",
      "74 : 15\n",
      "30 : 14\n",
      "57 : 5\n",
      "30 : 14\n",
      "6 : 13\n",
      "20 : 8\n",
      "61 : 13\n",
      "27 : 12\n",
      "55 : 18\n",
      "70 : 9\n",
      "20 : 8\n",
      "13 : 9\n",
      "15 : 13\n",
      "23 : 13\n",
      "95 : 12\n",
      "71 : 10\n",
      "94 : 10\n",
      "83 : 8\n",
      "55 : 18\n",
      "19 : 6\n",
      "88 : 8\n",
      "29 : 9\n",
      "31 : 10\n",
      "97 : 8\n",
      "55 : 18\n",
      "6 : 13\n",
      "36 : 15\n",
      "51 : 9\n",
      "95 : 12\n",
      "76 : 5\n",
      "80 : 8\n",
      "48 : 6\n",
      "62 : 8\n",
      "12 : 12\n",
      "95 : 12\n",
      "54 : 13\n",
      "9 : 7\n",
      "69 : 10\n",
      "7 : 9\n",
      "35 : 11\n",
      "93 : 5\n",
      "36 : 15\n",
      "79 : 10\n",
      "8 : 14\n",
      "47 : 4\n",
      "24 : 3\n",
      "32 : 11\n",
      "75 : 7\n",
      "44 : 13\n",
      "17 : 9\n",
      "30 : 14\n",
      "29 : 9\n",
      "46 : 11\n",
      "46 : 11\n",
      "7 : 9\n",
      "39 : 11\n",
      "0 : 8\n",
      "91 : 7\n",
      "4 : 7\n",
      "49 : 11\n",
      "12 : 12\n",
      "94 : 10\n",
      "96 : 13\n",
      "21 : 9\n",
      "0 : 8\n",
      "36 : 15\n",
      "85 : 11\n",
      "71 : 10\n",
      "87 : 4\n",
      "48 : 6\n",
      "93 : 5\n",
      "54 : 13\n",
      "49 : 11\n",
      "99 : 20\n",
      "22 : 13\n",
      "54 : 13\n",
      "91 : 7\n",
      "70 : 9\n",
      "49 : 11\n",
      "7 : 9\n",
      "72 : 15\n",
      "89 : 16\n",
      "90 : 7\n",
      "3 : 9\n",
      "79 : 10\n",
      "94 : 10\n",
      "71 : 10\n",
      "54 : 13\n",
      "99 : 20\n",
      "74 : 15\n",
      "80 : 8\n",
      "23 : 13\n",
      "22 : 13\n",
      "51 : 9\n",
      "33 : 10\n",
      "52 : 10\n",
      "44 : 13\n",
      "36 : 15\n",
      "99 : 20\n",
      "37 : 11\n",
      "32 : 11\n",
      "15 : 13\n",
      "47 : 4\n",
      "99 : 20\n",
      "51 : 9\n",
      "59 : 8\n",
      "41 : 12\n",
      "50 : 11\n",
      "48 : 6\n",
      "0 : 8\n",
      "2 : 12\n",
      "42 : 11\n",
      "15 : 13\n",
      "35 : 11\n",
      "2 : 12\n",
      "99 : 20\n",
      "64 : 20\n",
      "28 : 5\n",
      "64 : 20\n",
      "58 : 7\n",
      "63 : 13\n",
      "32 : 11\n",
      "90 : 7\n",
      "55 : 18\n",
      "6 : 13\n",
      "74 : 15\n",
      "16 : 10\n",
      "24 : 3\n",
      "1 : 7\n",
      "15 : 13\n",
      "66 : 13\n",
      "43 : 8\n",
      "3 : 9\n",
      "83 : 8\n",
      "86 : 11\n",
      "71 : 10\n",
      "77 : 6\n",
      "89 : 16\n",
      "29 : 9\n",
      "36 : 15\n",
      "73 : 13\n",
      "27 : 12\n",
      "62 : 8\n",
      "89 : 16\n",
      "27 : 12\n",
      "63 : 13\n",
      "59 : 8\n",
      "43 : 8\n",
      "32 : 11\n",
      "75 : 7\n",
      "98 : 14\n",
      "90 : 7\n",
      "79 : 10\n",
      "26 : 9\n",
      "53 : 9\n",
      "30 : 14\n",
      "16 : 10\n",
      "9 : 7\n",
      "66 : 13\n",
      "25 : 10\n",
      "16 : 10\n",
      "17 : 9\n",
      "39 : 11\n",
      "31 : 10\n",
      "13 : 9\n",
      "23 : 13\n",
      "99 : 20\n",
      "2 : 12\n",
      "20 : 8\n",
      "74 : 15\n",
      "64 : 20\n",
      "80 : 8\n",
      "50 : 11\n",
      "66 : 13\n",
      "41 : 12\n",
      "53 : 9\n",
      "31 : 10\n",
      "83 : 8\n",
      "65 : 4\n",
      "22 : 13\n",
      "29 : 9\n",
      "46 : 11\n",
      "58 : 7\n",
      "78 : 11\n",
      "30 : 14\n",
      "84 : 10\n",
      "18 : 8\n",
      "27 : 12\n",
      "66 : 13\n",
      "95 : 12\n",
      "100 : 7\n",
      "2 : 12\n",
      "80 : 8\n",
      "92 : 6\n",
      "36 : 15\n",
      "95 : 12\n",
      "77 : 6\n",
      "70 : 9\n",
      "85 : 11\n",
      "2 : 12\n",
      "99 : 20\n",
      "4 : 7\n",
      "32 : 11\n",
      "99 : 20\n",
      "85 : 11\n",
      "45 : 13\n",
      "16 : 10\n",
      "51 : 9\n",
      "10 : 10\n",
      "75 : 7\n",
      "64 : 20\n",
      "95 : 12\n",
      "82 : 9\n",
      "34 : 11\n",
      "60 : 14\n",
      "25 : 10\n",
      "66 : 13\n",
      "72 : 15\n",
      "4 : 7\n",
      "28 : 5\n",
      "38 : 6\n",
      "32 : 11\n",
      "2 : 12\n",
      "79 : 10\n",
      "71 : 10\n",
      "37 : 11\n",
      "22 : 13\n",
      "84 : 10\n",
      "33 : 10\n",
      "25 : 10\n",
      "44 : 13\n",
      "36 : 15\n",
      "14 : 9\n",
      "64 : 20\n",
      "72 : 15\n",
      "8 : 14\n",
      "49 : 11\n",
      "19 : 6\n",
      "86 : 11\n",
      "73 : 13\n",
      "31 : 10\n",
      "21 : 9\n",
      "19 : 6\n",
      "86 : 11\n",
      "39 : 11\n",
      "71 : 10\n",
      "81 : 8\n",
      "8 : 14\n",
      "17 : 9\n",
      "50 : 11\n",
      "92 : 6\n",
      "1 : 7\n",
      "77 : 6\n",
      "44 : 13\n",
      "61 : 13\n",
      "79 : 10\n",
      "41 : 12\n",
      "74 : 15\n",
      "43 : 8\n",
      "64 : 20\n",
      "54 : 13\n",
      "56 : 6\n",
      "73 : 13\n",
      "25 : 10\n",
      "45 : 13\n",
      "21 : 9\n",
      "64 : 20\n",
      "70 : 9\n",
      "18 : 8\n",
      "89 : 16\n",
      "98 : 14\n",
      "55 : 18\n",
      "61 : 13\n",
      "28 : 5\n",
      "63 : 13\n",
      "83 : 8\n",
      "2 : 12\n",
      "2 : 12\n",
      "33 : 10\n",
      "99 : 20\n",
      "100 : 7\n",
      "34 : 11\n",
      "62 : 8\n",
      "60 : 14\n",
      "5 : 8\n",
      "55 : 18\n",
      "88 : 8\n",
      "62 : 8\n",
      "56 : 6\n",
      "73 : 13\n",
      "58 : 7\n",
      "68 : 7\n",
      "30 : 14\n",
      "81 : 8\n",
      "84 : 10\n",
      "77 : 6\n",
      "18 : 8\n",
      "64 : 20\n",
      "49 : 11\n",
      "10 : 10\n",
      "61 : 13\n",
      "11 : 11\n",
      "70 : 9\n",
      "98 : 14\n",
      "29 : 9\n",
      "33 : 10\n",
      "61 : 13\n",
      "50 : 11\n",
      "53 : 9\n",
      "57 : 5\n",
      "80 : 8\n",
      "21 : 9\n",
      "54 : 13\n",
      "36 : 15\n",
      "89 : 16\n",
      "72 : 15\n",
      "94 : 10\n",
      "8 : 14\n",
      "67 : 9\n",
      "71 : 10\n",
      "47 : 4\n",
      "38 : 6\n",
      "63 : 13\n",
      "92 : 6\n",
      "96 : 13\n",
      "7 : 9\n",
      "69 : 10\n",
      "54 : 13\n",
      "30 : 14\n",
      "37 : 11\n",
      "58 : 7\n",
      "84 : 10\n",
      "39 : 11\n",
      "95 : 12\n",
      "6 : 13\n",
      "74 : 15\n",
      "33 : 10\n",
      "46 : 11\n",
      "12 : 12\n",
      "11 : 11\n",
      "36 : 15\n",
      "32 : 11\n",
      "8 : 14\n",
      "91 : 7\n",
      "74 : 15\n",
      "54 : 13\n",
      "80 : 8\n",
      "16 : 10\n",
      "22 : 13\n",
      "12 : 12\n",
      "77 : 6\n",
      "62 : 8\n",
      "22 : 13\n",
      "44 : 13\n",
      "78 : 11\n",
      "37 : 11\n",
      "89 : 16\n",
      "57 : 5\n",
      "6 : 13\n",
      "41 : 12\n",
      "7 : 9\n",
      "37 : 11\n",
      "11 : 11\n",
      "41 : 12\n",
      "72 : 15\n",
      "60 : 14\n",
      "31 : 10\n",
      "22 : 13\n",
      "20 : 8\n",
      "2 : 12\n",
      "100 : 7\n",
      "56 : 6\n",
      "23 : 13\n",
      "97 : 8\n",
      "72 : 15\n",
      "37 : 11\n",
      "36 : 15\n",
      "61 : 13\n",
      "50 : 11\n",
      "85 : 11\n",
      "40 : 9\n",
      "69 : 10\n",
      "97 : 8\n",
      "88 : 8\n",
      "54 : 13\n",
      "40 : 9\n",
      "52 : 10\n",
      "67 : 9\n",
      "89 : 16\n",
      "79 : 10\n",
      "6 : 13\n",
      "11 : 11\n",
      "69 : 10\n",
      "50 : 11\n",
      "94 : 10\n",
      "26 : 9\n",
      "60 : 14\n",
      "4 : 7\n",
      "17 : 9\n",
      "30 : 14\n",
      "52 : 10\n",
      "73 : 13\n",
      "27 : 12\n",
      "74 : 15\n",
      "86 : 11\n",
      "51 : 9\n",
      "78 : 11\n",
      "18 : 8\n",
      "82 : 9\n",
      "85 : 11\n",
      "43 : 8\n",
      "89 : 16\n",
      "23 : 13\n",
      "88 : 8\n",
      "35 : 11\n",
      "20 : 8\n",
      "55 : 18\n",
      "5 : 8\n",
      "5 : 8\n",
      "22 : 13\n",
      "44 : 13\n",
      "11 : 11\n",
      "7 : 9\n",
      "27 : 12\n",
      "66 : 13\n",
      "35 : 11\n",
      "32 : 11\n",
      "52 : 10\n",
      "62 : 8\n",
      "8 : 14\n",
      "76 : 5\n",
      "69 : 10\n",
      "34 : 11\n",
      "83 : 8\n",
      "4 : 7\n",
      "23 : 13\n",
      "30 : 14\n",
      "73 : 13\n",
      "59 : 8\n",
      "89 : 16\n",
      "27 : 12\n",
      "14 : 9\n",
      "41 : 12\n",
      "82 : 9\n",
      "58 : 7\n",
      "50 : 11\n",
      "10 : 10\n",
      "16 : 10\n",
      "9 : 7\n",
      "63 : 13\n",
      "82 : 9\n",
      "84 : 10\n",
      "8 : 14\n",
      "17 : 9\n",
      "39 : 11\n",
      "49 : 11\n",
      "5 : 8\n",
      "22 : 13\n",
      "61 : 13\n",
      "98 : 14\n",
      "6 : 13\n",
      "63 : 13\n",
      "4 : 7\n",
      "53 : 9\n",
      "52 : 10\n",
      "26 : 9\n",
      "18 : 8\n",
      "80 : 8\n",
      "96 : 13\n",
      "80 : 8\n",
      "25 : 10\n",
      "74 : 15\n",
      "66 : 13\n",
      "27 : 12\n",
      "26 : 9\n",
      "11 : 11\n",
      "31 : 10\n",
      "73 : 13\n",
      "95 : 12\n",
      "3 : 9\n",
      "94 : 10\n",
      "26 : 9\n",
      "64 : 20\n",
      "14 : 9\n",
      "0 : 8\n",
      "5 : 8\n",
      "79 : 10\n",
      "66 : 13\n",
      "99 : 20\n",
      "8 : 14\n",
      "31 : 10\n",
      "29 : 9\n",
      "68 : 7\n",
      "43 : 8\n",
      "86 : 11\n",
      "38 : 6\n",
      "1 : 7\n",
      "32 : 11\n",
      "23 : 13\n",
      "40 : 9\n",
      "78 : 11\n",
      "49 : 11\n",
      "6 : 13\n",
      "5 : 8\n",
      "13 : 9\n",
      "30 : 14\n",
      "3 : 9\n",
      "12 : 12\n",
      "82 : 9\n",
      "40 : 9\n",
      "34 : 11\n",
      "59 : 8\n",
      "43 : 8\n",
      "12 : 12\n",
      "100 : 7\n",
      "43 : 8\n",
      "15 : 13\n",
      "83 : 8\n"
     ]
    }
   ],
   "source": [
    "import random                      \n",
    "y=[random.randint(0,100)for i in range(1000)]\n",
    "for i in y:\n",
    "    print(i,':',y.count(i))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "16.编写程序,用户输人一个列表和2个整数作为下标,然后输出列表中介于2个下标闭区间之间的元素组成的子列表。例如用户输人[1,2,3,4,5,6]和2,5,程序输出[3,4,5,6]。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一个列表[1,2,3,4,5,6]\n"
     ]
    }
   ],
   "source": [
    "list=eval(input('请输入一个列表'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入第一个下标: 2\n"
     ]
    }
   ],
   "source": [
    "a=eval(input(\"请输入第一个下标: \"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入第一个下标: 5\n"
     ]
    }
   ],
   "source": [
    "b=eval(input(\"请输入第二个下标: \"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3, 4, 5]\n"
     ]
    }
   ],
   "source": [
    "if a>b:\n",
    "    for i in range(b,a):\n",
    "        print(list[b:a])\n",
    "        break\n",
    "elif a<b:\n",
    "    for i in range(a,b):\n",
    "        print(list[a:b])\n",
    "        break\n",
    "elif a==b:\n",
    "    print([])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "17.设计一个字典,并编写程序,用户输人内容作为“键”,然后输出字典中对应的“值”,如果用户输人的“键”不存在,则输出“您输人的键不存在!”。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "18.编写程序,生成包含20个随机数的列表,然后将前10个元素升序排列,后10 个元素降序排列,并输出结果。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "165px"
   },
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
